/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.vidmt.api.modules.sys.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vidmt.api.common.anno.ApiDoc;
import com.vidmt.api.common.exceptions.BadParamException;
import com.vidmt.api.common.exceptions.UserNotExistsException;
import com.vidmt.api.common.pojo.ApiResultServer;
import com.vidmt.api.common.utils.AES128Coder;
import com.vidmt.api.common.utils.StrUtil;
import com.vidmt.api.common.web.ApiController;
import com.vidmt.api.modules.sys.Acc;
import com.vidmt.api.modules.sys.Enums.AccType;
import com.vidmt.api.modules.sys.entity.User;
import com.vidmt.api.modules.sys.service.ImService;
import com.vidmt.api.modules.sys.service.UserService;
import com.vidmt.api.modules.sys.shiro.ShiroUtil;
import com.vidmt.api.modules.sys.utils.VUtil;

/**
 * 用户Controller
 * 
 * @author ThinkGem
 * @version 2013-8-29
 */
@Controller
@ResponseBody
@RequestMapping(value = "/api/1/user")
public class ApiUserController extends ApiController {
	@Autowired
	private UserService userService;

	@ApiDoc({ "newpwd",	"按照utf8转换成字节后用aes加密，其key=uid.toString().getbytes(utf8)。" })
	@ApiDoc({ "os", "android=a/ios=i" })
	@RequestMapping("/changepassword.*")
	public ApiResultServer changePassword(String newpwd) throws UserNotExistsException {
		User user = ShiroUtil.getCurrentUser();
		Long uid = user.getId();
		if (uid == null) {
			throw new UserNotExistsException(user.getDisplayName() + " not exitst");
		}
		AES128Coder coder = new AES128Coder(StrUtil.bytesutf8(uid.toString()));
		newpwd = StrUtil.newutf8(coder.decrypt(newpwd));
		userService.changePwd(newpwd, user.getId());
		return new ApiResultServer();
	}

	@ApiDoc({ "可能泄露隐私，待升级", "" })
	@RequestMapping("/public/resetpassword.*")
	public ApiResultServer resetPassword(String acctxt, String os, Integer ver)
			throws UserNotExistsException, BadParamException {
		String key = os + ver;
		String[] accarr = VUtil.decodeAcctxt(acctxt, key);

		String pwd = accarr[2];
		Acc acc = new Acc(AccType.valueOf(accarr[0]), accarr[1]);
		User user = userService.findByAcc(acc);
		if (user == null) {
			throw new UserNotExistsException("acc:[" + acc + "] not exitst");
		}
		userService.changePwd(pwd, user.getId());
		return new ApiResultServer();
	}

	/**
	 * 查找陌生人
	 */
	@ApiDoc({ "可能泄露隐私，待升级", "" })
	@RequestMapping("/getbyaccount.*")
	public ApiResultServer getbyaccount(AccType acctype, String accvalue) throws BadParamException {
		User user = userService.findByAcc(new Acc(acctype, accvalue));
		if (user != null) {
			userService.checkLvlttl(user);
		}
		return new ApiResultServer(user);
	}

	@ApiDoc({ "可能泄露隐私，待升级", "" })
	@RequestMapping("/listbyuids.*")
	public ApiResultServer findbyuids(Long[] uids) {
		List<User> users = userService.findByUids(uids);
		for (User user : users) {
			userService.checkLvlttl(user);
		}
		return new ApiResultServer(users);
	}

	@RequestMapping("/update.*")
	public ApiResultServer update(User user) throws BadParamException {
		User currentuser = ShiroUtil.getCurrentUser();
		boolean valid = false;
		if (user.getAddress() != null) {
			currentuser.setAddress(user.getAddress());
			valid = true;
		}
		if (user.getNick() != null) {
			currentuser.setNick(user.getNick());
			valid = true;
		}
		if (user.getSignature() != null) {
			currentuser.setSignature(user.getSignature());
			valid = true;
		}
		if (user.getBirth() != null) {
			currentuser.setBirth(user.getBirth());
			valid = true;
		}
		if (user.getSex() != null) {
			currentuser.setSex(user.getSex());
			valid = true;
		}
		if (user.getLocPrivate() != null) {
			currentuser.setLocPrivate(user.getLocPrivate());
			valid = true;
		}
		if (user.getAvoidDisturb() != null) {
			currentuser.setAvoidDisturb(user.getAvoidDisturb());
			valid = true;
		}
		if (!valid) {
			throw new BadParamException("参数错误，没有任何需要修改的");
		}
		userService.update(currentuser);
		return new ApiResultServer();
	}
}
